#!/bin/sh

# Slackware build script for fvwm3

# Copyright 2020-2021, Alexander Verbovetsky, Moscow, Russia
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

PRGNAM=fvwm3
VERSION=${VERSION:-1.0.2}
BUILD=${BUILD:-3}
TAG=${TAG:-_SBo}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION

patch -p1 < $CWD/update_fvwm_screen-dont_lose_desk_from_StartsOnScreen.patch

mkdir -p bin/FvwmPrompt/vendor/golang.org/x
for DEPENDENCY in \
  abiosoft/ishell/2.0.0 \
  abiosoft/readline/155bce2042db95a783081fab225e74dd879055b0 \
  fatih/color/1.9.0 \
  flynn-archive/go-shlex/3f9db97f856818214da2e1057f8ad84803971cff \
  mattn/go-colorable/0.1.4 \
  mattn/go-isatty/0.0.12 \
  sirupsen/logrus/1.6.0 \
  golang/sys/12a6c2dcc1e4cb348b57847c73987099e261714b
do
  DEPENDENCY_OWNER="$(echo "$DEPENDENCY" | cut -d/ -f1)"
  DEPENDENCY_NAME="$(echo "$DEPENDENCY" | cut -d/ -f2)"
  DEPENDENCY_REV="$(echo "$DEPENDENCY" | cut -d/ -f3)"
  mkdir -p "bin/FvwmPrompt/vendor/github.com/$DEPENDENCY_OWNER"
  tar xvf "$CWD/$DEPENDENCY_NAME-$DEPENDENCY_REV.tar.gz"
  if [ "$DEPENDENCY_OWNER" = "golang" ]; then
    mv "$DEPENDENCY_NAME-$DEPENDENCY_REV" \
      "bin/FvwmPrompt/vendor/golang.org/x/$DEPENDENCY_NAME"
  else
    mv "$DEPENDENCY_NAME-$DEPENDENCY_REV" \
      "bin/FvwmPrompt/vendor/github.com/$DEPENDENCY_OWNER/$DEPENDENCY_NAME"
  fi
done

cat $CWD/modules.txt > bin/FvwmPrompt/vendor/modules.txt

chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --enable-mandoc \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  --enable-golang \
  --program-prefix= \
  --program-suffix= \
  --program-transform-name='s/^fvwm-/fvwm3-/' \
  --build=$ARCH-slackware-linux

make
make install DESTDIR=$PKG

zcat $CWD/startfvwm3.gz > $PKG/usr/bin/startfvwm3
chmod 755 $PKG/usr/bin/startfvwm3

mkdir -p $PKG/etc/X11/xinit
zcat $CWD/xinitrc.fvwm3.gz > $PKG/etc/X11/xinit/xinitrc.fvwm3
chmod 755 $PKG/etc/X11/xinit/xinitrc.fvwm3

rm -rf $PKG/usr/share/doc
rm -rf $PKG/usr/share/locale
rm -f $PKG/usr/bin/fvwm3-convert-2.6
mv $PKG/usr/bin/FvwmCommand $PKG/usr/bin/Fvwm3Command
mv $PKG/usr/bin/xpmroot $PKG/usr/bin/xpmroot3

find $PKG -print0 | xargs -0 file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

find $PKG/usr/man -type f -exec gzip -9 {} \;
for i in $( find $PKG/usr/man -type l ) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done

for m in $PKG/usr/man/man1/Fvwm* ; do
  case "$m" in *FvwmPrompt*) continue ;; esac
  mv $m ${m/Fvwm/Fvwm3}
done

for m in $PKG/usr/man/man1/fvwm-* ; do
  mv $m ${m/fvwm-/fvwm3-}
done

rm -f $PKG/usr/man/man1/xpmroot*
ln -s fvwm3-root.1.gz $PKG/usr/man/man1/xpmroot3.1.gz

rm -f $PKG/usr/man/man1/fvwm3-convert-2.6.1.gz
rm -f $PKG/usr/man/man1/fvwm3-config-2.6.1.gz
rm -f $PKG/usr/man/man1/Fvwm3Console.1.gz
rm -f $PKG/usr/man/man1/Fvwm3ConsoleC.pl.1.gz

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a NEWS $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
